<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalDesignerModel;
use app\portal\service\PostService;
use think\Db;
use app\admin\model\ThemeModel;


class AdminDesignerController extends AdminBaseController
{
   public function index()
   {
       $param = $this->request->param();

       $postService = new PostService();
       $data        = $postService->adminPageList($param);
       $data->appends($param);

       $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
       $this->assign('pages', $data->items());
       $this->assign('page', $data->render());

       return $this->fetch();

   }

   
    /**
     * 添加设计师
     * @adminMenu(
     *     'name'   => '添加设计师',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设计师',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $themeModel        = new ThemeModel();
        $designerThemeFiles = $themeModel->getActionThemeFiles('portal/Designer/index');
        $this->assign('designer_theme_files', $designerThemeFiles);
        return $this->fetch();
    }
    /**
     * 添加设计师提交
     * @adminMenu(
     *     'name'   => '添加设计师提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设计师提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
//            $result = $this->validate($post, 'AdminArticle');
//            if ($result !== true) {
//                $this->error($result);
//            }

            $DesignerModel = new PortalDesignerModel();

            if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                $data['post']['more']['photos'] = [];
                foreach ($data['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
                }
            }

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }
            $DesignerModel->adminAddDesigner($data['post']);

            $data['post']['id'] = $DesignerModel->id;

            $this->success('添加成功!', '',['id' => $DesignerModel->id]);
        }

    }




}
